<?php
/**
* Entidad comentario.
*/
class Comment {
	
	CONST DB_COMMENT = "comment";
	CONST DB_COMMENT_ID = "idcomment";
	CONST DB_COMMENT_IDGROUP = "idgroup";
	CONST DB_COMMENT_IDPLACE = "idplace";
	CONST DB_COMMENT_AUTHOR = "autor";
	CONST DB_COMMENT_ACTIVE = "active";
	CONST DB_COMMENT_LASTUPDATE = "lastupdate";
	CONST DB_COMMENT_COMMENT = "comment";
	CONST DB_COMMENT_CREATED = "created";
	
	/**
	 * Identificador del comentario
	 * */
	private $idcomment;
	/**
	 * Identificador del grupo al que hace referencia.
	 * */
	private $idgroup;
	
	/**
	 * Identificador del lugar al que hace refrencia
	 * */
	private $idplace;
	
	/**
	 * Autor del comentario
	 * */
	private $author;
	
	/**
	 * Comentario
	 * */
	private $comment;
	
	private $lastupdate, $created;
	
	public function putLastUpdate($lu){
		$this->lastUpdate = $lu;
	}
	
	public function putId($id){
		$this->idcomment = $id;
		
	}
	
	public function __construct($idgroup, $author, $comment, $idplace = NULL){
		$this->idgroup = $idgroup;
		$this->idplace = $idplace;
		$this->author = $author;
		$this->comment = $comment;		
	}
	
	/**
	 * Guarda en la base de datos la información del comentario.
	 * */
	public function saveToDB(){
		$this->created = $this->lastupdate = time();
		$idplace = (!is_null($this->idplace))? $this->idplace: "null";
		$q = "INSERT INTO `".self::DB_COMMENT."` (".self::DB_COMMENT_AUTHOR.",".self::DB_COMMENT_COMMENT.",".self::DB_COMMENT_IDGROUP.",".self::DB_COMMENT_IDPLACE.",".self::DB_COMMENT_LASTUPDATE.",".self::DB_COMMENT_CREATED.") VALUES(".$this->author.",'".formsql(to_utf8($this->comment))."',".$this->idgroup.",".$idplace.",".$this->lastupdate.",".$this->created.")";
		$res = sendsql($q, true);
		
		$this->idcomment = $res['id'];
	}
	
	public function tojson(){
		$result = array(
			idOnline => $this->idcomment,
			//idgroup => $this->idgroup,
			idAutor => $this->author,
			idPlaceRelated => $this->idplace,
			lastUpdate => $this->lastupdate,
			comment => $this->comment,
			//created => $this->created
		);
		
		return $result;
	}
	
}
	
?>
	